/*
 * edbtool2_help.c - 
 * Clunix, cezanne@clunix.com, 2004.12.20.
 *
 * Copyright (c) 2001-2008 by Clunix Inc. All Rights Reserved.
 */

#include <stdio.h>
#include <string.h>

#include "edbtool2.h"

static void
help_create(void)
{
	printf(
"create <CNP> <Type> [<ASRV>]\n"
"\n");
}

static void
help_get(void)
{
	printf(
"get <options> <CNP>\n"
"\n"
"<options>:\n"
" -owner: show owner\n"
" -reactor: show reactor\n"
" -l: do not dereference\n"
" -r: recusive\n");
}

static void
help_set(void)
{
	printf(
"set <options> <CNP> <ASRV>\n"
"\n"
"<options>:\n"
" -l: do not dereference\n");
}

static void
help_asrget(void)
{
	printf(
"asrget <options> <CNP> <filepath>\n"
"\n"
"<options>:\n"
" -l: do not dereference\n"
"\n"
"<filepath>: file path where ASR will be saved\n");
}

static void
help_asrset(void)
{
	printf(
"asrset <CNP> <filepath>\n"
"\n"
"<filepath>: file path of stored ASR\n");
}

static void
help_xecreate(void)
{
	printf(
"xecreate <CNP> [<xe name> [<xe type>]]\n"
"\n"
"<xe_name>: XE name. Created anonymously if not given\n"
"\n"
"<xe type>: XE type name. Created with default type of HMS if not given\n"
	);
}

static void
help_xedelete(void)
{
	printf(
"xedelete [<options>] <CNP>\n"
"\n"
"<options>:\n"
" -f: delete forcefully if it has sub XEs\n"
"\n"
	);
}

static void
help_shutdown(void)
{
	printf(
"shutdown [<LNP>]\n"
"\n"
"<LNP>: <node> LNP to be shutdown\n"
	);
}

static void
help_rename(void)
{
	printf(
"rename <CNP> <LNP>\n"
" maybe not suppported\n"
	);
}

static void
help_del(void)
{
	printf(
"del <options> <CNP>\n"
	);
}

static void
help_snapshot(void)
{
	printf(
"snapshot\n"
" snapshot EDB root\n"
	);
}

static void
help_elect(void)
{
	printf(
"elect\n"
" elect new master\n"
	);
}

static void
help_cd(void)
{
	printf(
"cd <LNP>\n"
"\n"
"<LNP>: LNP of new working directory\n"
	);
}

static void
help_ls(void)
{
	printf(
"ls\n"
" listing entries\n"
	);
}

static void
help_get_inherited_types(void)
{
	printf(
"get_inherited_types\n"
	);
}

static void
help_echo(void)
{
	printf(
"echo <msg>\n"
"\n"
"<msg>: message to be echoed\n"
	);
}

static void
help_prompt(void)
{
	printf(
"prompt <msg>\n"
"\n"
"<msg>: prompt string\n"
	);
}

static void
help_exec(void)
{
	printf(
"exec [ <EDExpression> ]\n"
" execute ED expression. Square brackets are token(not optional meaning)\n"
	);
}

static void
help_quit(void)
{
	printf(
"quit\n"
" Quit edbtool2\n"
	);
}

static void
help_help(void)
{
	printf(
"help [<subject>]\n"
	);
}

static void
help_top(void)
{
	printf(
"edbtool2 supports following commands:\n"
"\n"
"create  : create edb entry\n"
"get     : get edb entry\n" 
"set     : set edb entry\n"
"asrget  : get edb entry with ASR format\n"
"asrset  : set edb entry with ASR format\n"
"xecreate: create XE\n"
"xedelete: delete XE\n"
"shutdown: shutdown EDB\n"
"rename  : rename edb entry\n"
"del     : delete edb entry\n"
"snapshot: snapshot EDB data\n"
"elect   : elect new master\n"
"help    : show help message\n"
"cd      : change directory\n"
"ls      : just list entries\n"
"get_inherited_types: get inherited types\n"
"echo    : healthness check\n"
"prompt  : prompt\n"
"exec    : execute ED expression\n"
"quit    : quit edbtool2\n"
"\n"
"  Use \"help\" with command name for detailed each command.\n"
"\n"
"  Argument Notation:\n"
"  <Type>  : <type name> eg. <int> <str>\n"
"  <LNP>   : Logical(Long) Name Path\n"
"  <CNP>   : Conditional Name Path\n"
"            <LNP> ? [ <ED Expression> ]\n"
"\n");
}

static const struct helpinfo {
	const char *subject;
	void (*help)(void);
} helpinfos[] = {
	{ "create", help_create },
	{ "get", help_get },
	{ "set", help_set },
	{ "asrget", help_asrget },
	{ "asrset", help_asrset },
	{ "xecreate", help_xecreate },
	{ "xedelete", help_xedelete },
	{ "shutdown", help_shutdown },
	{ "rename", help_rename },
	{ "del", help_del },
	{ "snapshot", help_snapshot },
	{ "elect", help_elect },
	{ "help", help_help },
	{ "cd", help_cd },
	{ "ls", help_ls },
	{ "get_inherited_types", help_get_inherited_types },
	{ "echo", help_echo },
	{ "prompt", help_prompt },
	{ "exec", help_exec },
        { "quit", help_quit },
	{ NULL, NULL }
};

void
command_help(sr_t *sr_cmd)
{
	const struct	helpinfo	*hi = helpinfos;
	const char	*subject = sr_geti_str(sr_cmd, 1);

	if (ecm_empty_str(subject)) {
		help_top();
		return;
	}
	while (hi->subject) {
		if (strcmp(hi->subject, subject) == 0) {
			printf("\nUsage: ");
			hi->help();
			printf("\n");
			return;
		}
		hi++;
	}
	printf("%s: unknown command\n", subject);
}
